<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    #myCanvas{
      display: block;
      border: 1px solid #000;
      margin: 50px auto;
    }
    
  </style>
</head>
<body>
  <canvas width="500" height="500" id="myCanvas">
    当前版本浏览器不支持，请升级浏览器
  </canvas>
  <script>
    class Rect{
      constructor(x,y,w,h,color){
        this.x=x;
        this.y=y;
        this.w=w;
        this.h=h;
        this.color=color;
      }
      update(){
        this.x++
      }
      render(){
        ctx.fillStyle=this.color;
      }
    }
    //得到canvas的画布
    let canvas=document.getElementById('myCanvas');
    //得到画布的上下文，2d和3d
    let ctx=canvas.getContext('2d');
    var r1=new Rect(100,100,50,50,"purple");
    setInterval(() => {
      ctx.clearRect(0,0,canvas.width,canvas.height);
      r1.update()
      r1.render()
    },10);
    
  </script>
</body>
</html>